package com.csu.cphone.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csu.common.vo.ResultVo;
import com.csu.cphone.pojo.MaintainPhonePojo;
import com.csu.cphone.pojo.MaintainProblemPojo;
import com.csu.cphone.pojo.MaintainProblemSelectPojo;
import com.csu.cphone.service.MaintainPhoneService;
import com.csu.cphone.service.MaintainService;
import com.csu.cphone.vo.MaintainPhoneTreeVo;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * @项目名称：cphone
 * @类名称：MaintainController
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年10月25日 上午10:55:05
 * @company:
 */
@Controller
@RequestMapping("/maintain")
@Api(value = "maintain", description = "手机维修")
public class MaintainController
{

    @Autowired
    private MaintainService maintainService;

    @Autowired
    private MaintainPhoneService maintainPhoneService;

    @ResponseBody
    @ApiOperation(value = "获取手机维修故障", notes = "根据手机型号id获取维修用到的问题")
    @RequestMapping(value = "/getMaintainProblems/{phoneId}", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<MaintainProblemPojo>> getMaintainProblems(HttpServletRequest request,
            @ApiParam(value = "手机id") @PathVariable(value = "phoneId", required = true) int phoneId)
    {
        ResultVo<List<MaintainProblemPojo>> result = new ResultVo<List<MaintainProblemPojo>>();
        List<MaintainProblemPojo> problems = maintainService.getMaintainProblemsByPhoneId(phoneId);
        result.setData(problems);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "报价结果", notes = "根据维修问题计算报价")
    @RequestMapping(value = "/offer", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<Double> offer(HttpServletRequest request,
            @RequestBody List<MaintainProblemSelectPojo> problemSelects)
    {
        ResultVo<Double> result = new ResultVo<Double>();

        Double offer = maintainService.offer(problemSelects);
        result.setData(offer);
        return result;
    }

    /*
     * @ResponseBody
     * 
     * @ApiOperation(value = "添加维修问题", notes = "添加手机维修需要回答的问题")
     * 
     * @RequestMapping(value = "/addMaintainProblem", method = {
     * RequestMethod.POST, RequestMethod.GET }, produces = {
     * "application/json; charset=utf-8" }) public ResultVo<MaintainProblemPojo>
     * addMaintainProblem(HttpServletRequest request,
     * 
     * @RequestBody MaintainProblemPojo phoneProblem) {
     * ResultVo<MaintainProblemPojo> result = new
     * ResultVo<MaintainProblemPojo>(); MaintainProblemPojo problem =
     * maintainService.addMaintainProblem(phoneProblem);
     * result.setData(problem); return result; }
     */

    @ResponseBody
    @ApiOperation(value = "获取可维修机型", notes = "获取可维修机型列表树")
    @RequestMapping(value = "/getMaintainPhones", method = RequestMethod.GET, produces = { "application/json; charset=utf-8" })
    public ResultVo<List<MaintainPhoneTreeVo>> getMaintainPhones(HttpServletRequest request,
            HttpServletResponse response)
    {
        ResultVo<List<MaintainPhoneTreeVo>> result = new ResultVo<List<MaintainPhoneTreeVo>>();

        List<MaintainPhoneTreeVo> treeList = maintainService.getMaintainPhoneTreeList();

        result.setData(treeList);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "添加维修机手机", notes = "添加维修机的手机")
    @RequestMapping(value = "/addMaintainPhone", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<MaintainPhonePojo> addMaintainPhone(HttpServletRequest request, HttpServletResponse response,
            @ApiParam(value = "品牌id") @RequestParam(value = "brandId", required = true) int brandId,
            @ApiParam(value = "手机名称") @RequestParam(value = "name", required = true) String name,
            @ApiParam(value = "图片路径") @RequestParam(value = "url", required = true) String url)
    {
        ResultVo<MaintainPhonePojo> result = new ResultVo<MaintainPhonePojo>();
        MaintainPhonePojo phone = maintainService.addPhone(brandId, name, url);
        result.setData(phone);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "查询维修机手机信息", notes = "获取维修手机信息")
    @RequestMapping(value = "/maintainPhoneInfo/{maintainPhoneId}", method = { RequestMethod.POST, RequestMethod.GET }, produces = { "application/json; charset=utf-8" })
    public ResultVo<MaintainPhonePojo> maintainPhoneInfo(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "维修机手机id") @PathVariable(value = "maintainPhoneId", required = true) Integer maintainPhoneId)
    {
        ResultVo<MaintainPhonePojo> result = new ResultVo<MaintainPhonePojo>();
        MaintainPhonePojo phone = maintainPhoneService.queryById(maintainPhoneId);
        result.setData(phone);
        return result;
    }

}
